//
//  ViewController.swift
//  CoreAnimation
//
//  Created by Yinyin Zeng on 23/5/20.
//  Copyright © 2020 MaoYeYeDeJiangCuCha. All rights reserved.
//

import UIKit
//核心动画
class ViewController: UIViewController {
    
    let grayView = UIView.init(frame: CGRect(x: 100, y: 100, width: 20, height: 20))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let rView = roundView.init(frame: self.view.frame)
        rView.backgroundColor = .white
        self.view.addSubview(rView)
        
        grayView.backgroundColor = .lightGray
        self.view.addSubview(grayView)
        self.animationGrouplayer(layer: grayView.layer)
        
    }
    
    
    //MARK: -CABasicAnimation
    //移动动画position
    func addLayerAnimationPosition(layer: CALayer) {
        let animation = CABasicAnimation(keyPath: "position")
        //开始的位置
        animation.fromValue = NSValue(cgPoint: layer.position)
        //移动到的位置
        animation.toValue = NSValue(cgPoint: CGPoint(x: 120, y: 200))
        //在自身的基础上进行累加
        //animation.byValue
        //持续时间
        animation.duration = 3
        animation.repeatCount = 2
        //运动后的位置保持不变（layer的最后位置是toValue）
        animation.isRemovedOnCompletion = false
        animation.fillMode = CAMediaTimingFillMode.forwards
        
        //添加动画
        layer.add(animation, forKey: "addLayerAnimationPosition")
    }
    
    //透明度 opacity
    func addLayerAnimationOpacity(layer: CALayer) {
        let animation = CABasicAnimation(keyPath: "opacity")
        animation.fromValue = 1
        animation.toValue = 0
        animation.duration = 3
        animation.repeatCount = 2
        layer.add(animation, forKey: "addLayerAnimationOpacity")
        
    }
    
    //变大与位置 bounds
    func addLayerAnimationBounds(layer:CALayer) {
        let animation = CABasicAnimation(keyPath: "bounds")
        animation.toValue = NSValue(cgRect:CGRect(x: 130, y: 200, width: 140, height: 140))
        animation.duration = 3
        animation.repeatCount = 1
        layer.add(animation, forKey: "addLayerAnimationBounds")
    }
    
    //由小变大 bounds.size
    func addLayerAnimationBoundsSize(layer:CALayer) {
        let animation = CABasicAnimation(keyPath: "bounds.size")
        animation.fromValue = NSValue(cgSize: layer.bounds.size)
        animation.toValue = NSValue(cgSize:CGSize(width: layer.bounds.size.width+20, height: layer.bounds.size.height+20))
        animation.duration = 3
        animation.repeatCount = 1
        layer.add(animation, forKey: "addLayerAnimationBoundsSize")
    }
    
    //改变颜色 backgroundColor
    func addLayerAnimationBackgroundColor(layer:CALayer) {
        let animation = CABasicAnimation(keyPath: "backgroundColor")
        animation.toValue = UIColor.blue.cgColor
        animation.duration = 3
        animation.repeatCount = 1
        
        layer.add(animation, forKey: "addLayerAnimationMargin")
    }
    
    //渐变圆角 cornerRadius
    func addLayerAnimationCornerRadius(layer:CALayer) {
        let animation = CABasicAnimation(keyPath: "cornerRadius")
        animation.toValue = 30
        animation.duration = 3
        animation.repeatCount = 2
        
        layer.add(animation, forKey: "addLayerAnimationCornerRadius")
    }
    
    
    //改变边框border的大小(图形周围边框，border默认为黑色)， borderWidth
    func addLayerAnimationBorderWidth(layer:CALayer) {
        let animation = CABasicAnimation(keyPath: "borderWidth")
        animation.toValue = 10
        animation.duration = 3
        animation.repeatCount = 1
        
        layer.add(animation, forKey: "addLayerAnimationBorderWidth")
    }
    
    
    //改变layer内容(图片)，注意如果想要达到改变内容的动画效果，首先在运行动画之前定义好layer的contents contents
    func addLayerAnimationContents(layer:CALayer) {
        let animation = CABasicAnimation(keyPath: "contents")
        let toImage = UIImage.init(named: "雪花")?.cgImage
        animation.toValue = toImage
        animation.duration = 3
        animation.repeatCount = 1
        
        layer.add(animation, forKey: "addLayerAnimationBounds")
    }
    
    
    //缩放、放大 transform.scale
    func addLayerAnimationTransformScale(layer:CALayer) {
        let animation = CABasicAnimation(keyPath: "transform.scale")
        //开始时的倍率
        animation.fromValue = 1.0
        //结束时的倍率
        animation.toValue = 0.5
        animation.duration = 3
        animation.repeatCount = 1
        animation.autoreverses = true
        
        layer.add(animation, forKey: "addLayerAnimationScale")
    }
    
    
    //旋转动画(翻转，沿着X轴) transform.rotation.x
    func addLayerAnimationTranformRotationX(layer: CALayer) {
        let animation = CABasicAnimation(keyPath: "transform.rotation.x")
        //旋转180度 = PI
        animation.toValue = Double.pi
        animation.duration = 3
        animation.repeatCount = 1
        //这里我们可以添加可以不添加，添加一个缓慢进出的动画效果(int/out)。当不添加时，匀速运动，会使用kCAMediaTimingFunctionLinear；当添加时，layer会在开始和结束时比较缓慢
        animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        
        layer.add(animation, forKey: "addLayerAnimationTranformRotationX")
    }
    
    
    //旋转动画(翻转，沿着Y轴) transform.rotation.y
    func addLayerAnimationTranformRotationY(layer: CALayer) {
        let animation = CABasicAnimation(keyPath: "transform.rotation.y")
        //旋转180度 = PI
        animation.toValue = Double.pi
        animation.duration = 3
        animation.repeatCount = 1
        //这里我们可以添加可以不添加，添加一个缓慢进出的动画效果(int/out)。当不添加时，匀速运动，会使用kCAMediaTimingFunctionLinear；当添加时，layer会在开始和结束时比较缓慢
        animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        
        layer.add(animation, forKey: "addLayerAnimationTranformRotationY")
    }
    
    
    //旋转动画（沿着Z轴） transform.rotation.z
    func addLayerAnimationTranformRotationZ(layer: CALayer) {
        let animation = CABasicAnimation(keyPath: "transform.rotation.z")
        //旋转360度 = PI2
        animation.toValue = Double.pi * 0.5
        animation.duration = 3
        animation.repeatCount = 1
        //这里我们可以添加可以不添加，添加一个缓慢进出的动画效果(int/out)。当不添加时，匀速运动，会使用kCAMediaTimingFunctionLinear；当添加时，layer会在开始和结束时比较缓慢
        animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        
        layer.add(animation, forKey: "addLayerAnimationTranformRotationZ")
    }
    
    
    //横向移动（沿着X轴） transform.translation.x
    func addLayerAnimationTranformTranslationX(layer: CALayer) {
        let animation = CABasicAnimation(keyPath: "transform.translation.x")
        animation.toValue = 20
        animation.duration = 3
        animation.repeatCount = 1
        //这里我们可以添加可以不添加，添加一个缓慢进出的动画效果(int/out)。当不添加时，匀速运动，会使用kCAMediaTimingFunctionLinear；当添加时，layer会在开始和结束时比较缓慢
        animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        
        layer.add(animation, forKey: "addLayerAnimationTranformTranslationX")
    }
    
    
    //纵向移动（沿着Y轴） transform.translation.y
    func addLayerAnimationTranformTranslationY(layer: CALayer) {
        let animation = CABasicAnimation(keyPath: "transform.translation.y")
        animation.toValue = 20
        animation.duration = 3
        animation.repeatCount = 1
        //这里我们可以添加可以不添加，添加一个缓慢进出的动画效果(int/out)。当不添加时，匀速运动，会使用kCAMediaTimingFunctionLinear；当添加时，layer会在开始和结束时比较缓慢
        animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        layer.add(animation, forKey: "addLayerAnimationTranformTranslationY")
    }
    
    
    //MARK: -CAKeyframeAnimation
    //关键针动画
    func keyframeAnimation(layer:CALayer){
        //1.做什么动画
        let keyframeAnimation = CAKeyframeAnimation(keyPath: "position")
//        keyframeAnimation.values = [CGPoint(x: 100,y: 100),CGPoint(x: 150,y: 100),CGPoint(x: 100,y: 150),CGPoint(x: 150,y: 150)]
        //还可以按照路径来执行
        let path = UIBezierPath.init(arcCenter: CGPoint(x: 150, y: 150), radius: 100, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)
        keyframeAnimation.path = path.cgPath
        
        keyframeAnimation.duration = 3
        keyframeAnimation.repeatCount = Float(INT_MAX) //接近无穷了啊
        
        
        
        layer.add(keyframeAnimation, forKey: "")
        
    }
    
    
    //MARK: -CAAnimationGroup
    func animationGrouplayer(layer:CALayer){
        //组动画
        let annimationGrop = CAAnimationGroup()
        
        //帧动画
        //1.做什么动画
        let keyframeAnimation = CAKeyframeAnimation(keyPath: "position")
        //        keyframeAnimation.values = [CGPoint(x: 100,y: 100),CGPoint(x: 150,y: 100),CGPoint(x: 100,y: 150),CGPoint(x: 150,y: 150)]
        //还可以按照路径来执行
        let path = UIBezierPath.init(arcCenter: CGPoint(x: 150, y: 150), radius: 100, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)
        keyframeAnimation.path = path.cgPath
        
        //基本动画
        let basicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        basicAnimation.byValue = (2 * Double.pi * 10)
        
        annimationGrop.duration = 3
        annimationGrop.repeatCount = Float(INT_MAX) //接近无穷了啊
        
        //需要操作的所有动画
        annimationGrop.animations = [keyframeAnimation,basicAnimation]
        
        //添加到
        layer.add(annimationGrop, forKey: "")
    }
    
    
}

